from flask import Flask,render_template, session, g
import config
from extends import db
from models import User, Product
from blueprints.centre import bp as centre_bp
from blueprints.auth import bp as auth_bp
from flask_migrate import Migrate

app=Flask(__name__)
#绑定配置文件
app.config.from_object(config)

#可以不要在创建的时候直接和app绑定
db.init_app(app)
migrate=Migrate(app, db)

#产生关联
app.register_blueprint(centre_bp)
app.register_blueprint(auth_bp)
#设置密钥
app.config["SECRET_KEY"] = 'TPmi4aLWRbyVq8zu9v82dWYW1'

@app.before_request
def my_before_request():
    username = session.get('username')
    if username:
        user = User.query.filter_by(username=username).first()
        setattr(g, "user", user)
    else:
        setattr(g, "user", None)

@app.context_processor
def my_context_processor():
    return { "user":g.user }



#修改host（局域网里让别人访问我们的项目，将host修改为0.0.0.0）
#修改port端口号，可能被别人占用
if __name__=='__main__':
    app.run(host="0.0.0.0",port=9000,debug=True)#debug模式打开——开发时，出现bug就可以在浏览器上看到信息